home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / glutduck / glutduck.c++ < prev    next >
C/C++ Source or Header  |  1996-11-11  |  6KB  |  271 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /*
  9.  * Copyright (c) 1991-94 Silicon Graphics, Inc.
  10.  *
  11.  * Permission to use, copy, modify, distribute, and sell this software and
  12.  * its documentation for any purpose is hereby granted without fee, provided
  13.  * that the name of Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Silicon Graphics.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  22.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  23.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  24.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  25.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26.  */
  27.  
  28. /* Based on an example from the Inventor Mentor chapter 13, example 5. */
  29.  
  30. #include <stdio.h>
  31. #include <unistd.h>
  32.  
  33. #include <GL/gl.h>
  34. #include <GL/glu.h>
  35. #include <GL/glut.h>
  36.  
  37. #include <Inventor/SoDB.h>
  38. #include <Inventor/SoInput.h>
  39. #include <Inventor/SbViewportRegion.h>
  40. #include <Inventor/nodes/SoSeparator.h>
  41. #include <Inventor/actions/SoGLRenderAction.h>
  42. #include <Inventor/nodes/SoCylinder.h>
  43. #include <Inventor/nodes/SoDirectionalLight.h>
  44. #include <Inventor/nodes/SoEventCallback.h>
  45. #include <Inventor/nodes/SoMaterial.h>
  46. #include <Inventor/nodes/SoPerspectiveCamera.h>
  47. #include <Inventor/nodes/SoRotationXYZ.h>
  48. #include <Inventor/nodes/SoTransform.h>
  49. #include <Inventor/nodes/SoTranslation.h>
  50.  
  51. int W = 300, H = 300;
  52. int spinning = 0;
  53. GLubyte *image = NULL;
  54. SoSeparator *root;
  55. SoRotationXYZ *duckRotXYZ;
  56. float angle = 0.0;
  57. int moving  = 0;
  58. int begin;
  59.  
  60. void
  61. reshape(int w, int h)
  62. {
  63.   glViewport(0, 0, w, h);
  64.   W = w;
  65.   H = h;
  66.   if (image)
  67.     free(image);
  68.   image = (GLubyte *) malloc(W * H * 3);
  69. }
  70.  
  71. void
  72. renderScene(void)
  73. {
  74.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  75.   SbViewportRegion myViewport(W, H);
  76.   SoGLRenderAction myRenderAction(myViewport);
  77.   myRenderAction.apply(root);
  78. }
  79.  
  80. void
  81. redraw(void)
  82. {
  83.   renderScene();
  84.   glutSwapBuffers();
  85. }
  86.  
  87. int
  88. duckScene(void)
  89. {
  90.    root = new SoSeparator;
  91.    root->ref();
  92.  
  93.    // Add a camera and light
  94.    SoPerspectiveCamera *myCamera = new SoPerspectiveCamera;
  95.    myCamera->position.setValue(0., -4., 8.0);
  96.    myCamera->heightAngle = M_PI/2.5; 
  97.    myCamera->nearDistance = 1.0;
  98.    myCamera->farDistance = 15.0;
  99.    root->addChild(myCamera);
  100.    root->addChild(new SoDirectionalLight);
  101.  
  102.    // Rotate scene slightly to get better view
  103.    SoRotationXYZ *globalRotXYZ = new SoRotationXYZ;
  104.    globalRotXYZ->axis = SoRotationXYZ::X;
  105.    globalRotXYZ->angle = M_PI/9;
  106.    root->addChild(globalRotXYZ);
  107.  
  108.    // Pond group
  109.    SoSeparator *pond = new SoSeparator; 
  110.    root->addChild(pond);
  111.    SoMaterial *cylMaterial = new SoMaterial;
  112.    cylMaterial->diffuseColor.setValue(0., 0.3, 0.8);
  113.    pond->addChild(cylMaterial);
  114.    SoTranslation *cylTranslation = new SoTranslation;
  115.    cylTranslation->translation.setValue(0., -6.725, 0.);
  116.    pond->addChild(cylTranslation);
  117.    SoCylinder *myCylinder = new SoCylinder;
  118.    myCylinder->radius.setValue(4.0);
  119.    myCylinder->height.setValue(0.5);
  120.    pond->addChild(myCylinder);
  121.  
  122.    // Duck group
  123.    SoSeparator *duck = new SoSeparator;
  124.    root->addChild(duck);
  125.  
  126.    // Read the duck object from a file and add to the group
  127.    SoInput myInput;
  128.    if (!myInput.openFile("duck.iv"))  {
  129.       if (!myInput.openFile("/usr/share/src/Inventor/examples/data/duck.iv")) {
  130.         return 1;
  131.       }
  132.    }
  133.    SoSeparator *duckObject = SoDB::readAll(&myInput);
  134.    if (duckObject == NULL) return 1;
  135.  
  136.    // Set up the duck transformations
  137.    duckRotXYZ = new SoRotationXYZ;
  138.    duck->addChild(duckRotXYZ);
  139.    duckRotXYZ->angle = angle;
  140.    duckRotXYZ->axis = SoRotationXYZ::Y;  // rotate about Y axis
  141.    SoTransform *initialTransform = new SoTransform;
  142.    initialTransform->translation.setValue(0., 0., 3.);
  143.    initialTransform->scaleFactor.setValue(6., 6., 6.);
  144.    duck->addChild(initialTransform);
  145.    duck->addChild(duckObject);
  146.  
  147.    return 0;
  148. }
  149.  
  150. void
  151. updateModels(void)
  152. {
  153.   duckRotXYZ->angle = angle;
  154.   glutPostRedisplay();
  155. }
  156.  
  157. void
  158. animate(void)
  159. {
  160.   angle += 0.1;
  161.   updateModels();
  162. }
  163.  
  164. void
  165. setAnimation(int enable)
  166. {
  167.   if(enable) {
  168.     spinning = 1;
  169.     glutIdleFunc(animate);
  170.   } else {
  171.     spinning = 0;
  172.     glutIdleFunc(NULL);
  173.     glutPostRedisplay();
  174.   }
  175. }
  176.  
  177. /* ARGSUSED */
  178. void
  179. keyboard(unsigned char ch, int x, int y)
  180. {
  181.   if(ch == ' ') {
  182.     setAnimation(0);
  183.     animate();
  184.   }
  185. }
  186.  
  187. void
  188. menuSelect(int item)
  189. {
  190.    switch(item) {
  191.    case 1:
  192.      animate();
  193.      break;
  194.    case 2:
  195.       if(!spinning) {
  196.             setAnimation(1);
  197.        } else {
  198.             setAnimation(0);
  199.        }
  200.       break;
  201.    }
  202. }
  203.  
  204. void
  205. vis(int visible)
  206. {
  207.   if (visible == GLUT_VISIBLE) {
  208.     if (spinning)
  209.       glutIdleFunc(animate);
  210.   } else {
  211.     if (spinning)
  212.       glutIdleFunc(NULL);
  213.   }
  214. }
  215.  
  216. /* ARGSUSED */
  217. void
  218. mouse(int button, int state, int x, int y)
  219. {
  220.   if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  221.     setAnimation(0);
  222.     moving = 1;
  223.     begin = x;
  224.   }
  225.   if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
  226.     moving = 0;
  227.     glutPostRedisplay();
  228.   }
  229. }
  230.  
  231. /* ARGSUSED */
  232. void
  233. motion(int x, int y)
  234. {
  235.   if (moving) {
  236.     angle = angle + .01 * (x - begin);
  237.     begin = x;
  238.     updateModels();
  239.   }
  240. }
  241.  
  242. void
  243. main(int argc, char **argv)
  244. {
  245.   glutInit(&argc, argv);
  246.   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  247.  
  248.   SoDB::init();
  249.  
  250.   if(duckScene()) {
  251.     fprintf(stderr, "couldn't read IV file\n");
  252.     exit(1);
  253.   }
  254.   glutInitWindowSize(W, H);
  255.   glutCreateWindow("GLUT Inventor Duck Pond");
  256.   glutDisplayFunc(redraw);
  257.   glutReshapeFunc(reshape);
  258.   glutCreateMenu(menuSelect);
  259.   glutAddMenuEntry("Step", 1);
  260.   glutAddMenuEntry("Toggle animation", 2);
  261.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  262.   glutKeyboardFunc(keyboard);
  263.   glutMouseFunc(mouse);
  264.   glutMotionFunc(motion);
  265.   glutVisibilityFunc(vis);
  266.   glEnable(GL_DEPTH_TEST);
  267.   glClearColor(0.132, 0.542, 0.132, 1.0);
  268.  
  269.   glutMainLoop();
  270. }
  271.